<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加新闻</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/add.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            background-color: #f4f4f4;
            font-family: Arial, sans-serif;
            overflow-x: hidden;
        }

        h2 {
            color: #333;
            display: flex;
            align-items: center;
        }

        h2 svg {
            margin-right: 10px;
        }

        .form-group {
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }

        .form-control {
            border-radius: 5px;
            border: 1px solid #ccc;
            padding: 10px;
            width: 100%;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            outline: none;
            border-color: #17a2b8;
            box-shadow: 0 0 5px rgba(23, 162, 184, 0.2);
            transform: scale(1.02);
        }

        .btn {
            border-radius: 5px;
            padding: 10px 20px;
            transition: all 0.3s ease;
        }

        .btn-info {
            background-color: #17a2b8;
            border: none;
            color: white;
        }

        .btn-info:hover {
            background-color: #138496;
            transform: translateY(-2px);
        }

        .btn-secondary {
            background-color: #6c757d;
            border: none;
            color: white;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-2px);
        }

        .form-inline {
            margin-bottom: 30px;
        }
    </style>
</head>

<body>

    <div class="container">

        <div class="form-inline">
            <h2 style="text-align: left; margin-left: 10px;">
                <i class="fa-solid fa-newspaper"></i>
                <span>添加新闻</span>
            </h2>
        </div>

        <form id="addNews">
            <div class="form-group">
                <label for="title">新闻标题:</label>
                <input type="text" class="form-control" placeholder="请输入新闻标题" id="title" name="title">
            </div>
            <div class="form-group">
                <label for="author">新闻作者</label>
                <input type="text" class="form-control" placeholder="请输入新闻作者" id="author" name="author" />
            </div>
            <div class="form-group">
                <label for="newsDate">新闻时间</label>
                <input type="text" class="form-control" placeholder="请输入新闻时间" id="newsDate" name="newsDate" />
            </div>
            <div class="form-group">
                <label for="newsContent">新闻内容</label>
                <textarea class="form-control" placeholder="请输入新闻内容" id="newsContent" name="newsContent"
                    rows="5"></textarea>
            </div>

            <div class="form-group">
                <label for="heat">新闻热度：</label>
                <input type="number" class="form-control" placeholder="请输入新闻热度" id="heat" name="heat">
            </div>


            <div class="form-group" style="text-align: right">
                <button type="button" class="btn btn-info btn-lg" onclick="add()">确定</button>
                <button type="button" class="btn btn-secondary btn-lg" onclick="javascript:history.back()">返回</button>
            </div>
        </form>
    </div>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script>
        function add() {
            //前端进行参数校验。和后端校验不冲突
            $.ajax({
                type: "post",
                url: "/news/addNews",
                data: $("#addNews").serialize(),
                success: function (result) {
                    if (result == "") {
                        //成功
                        alert("添加新闻");
                        location.href = "news.html";
                    } else {
                        alert("添加失败");
                    }

                },
                error: function (error) {
                    if (error.status == 401) {
                        //用户未登录
                        location.href = "login.html";
                    }
                }
            });
            // alert("添加成功");
            // location.href = "book_list.html";
        }
    </script>
</body>

</html>